package com.pl.lister;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.pl.entity.ErrologTb;
import com.pl.entity.LogType;
import com.pl.service.ErrologTbService;
import com.pl.service.LogTypeService;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Author 小胤
 * @Date 2024/5/5 0:31
 * @注释
 */
public class ErroLogReadLister extends AnalysisEventListener<ErrologTb> {
    private static final int BATCH_COUNT=5;
    private ErrologTbService errologTbService;
    List<ErrologTb> list=new ArrayList<>();

    public ErroLogReadLister() {
    }

    public ErroLogReadLister(ErrologTbService errologTbService) {
        this.errologTbService = errologTbService;
    }

    @Override
    public void invoke(ErrologTb errologTb, AnalysisContext analysisContext) {
        list.add(errologTb);
        if (list.size()>=BATCH_COUNT){
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (list.size()>0){
            saveData();
            list.clear();
        }
    }
    public void saveData(){
        System.out.println("开始批量导入数据");
        errologTbService.insertBatch(list);
        System.out.println("结束批量导入数据");
    }
}
